<?php

namespace brand\components;

use brand\logic\Config;
use brand\page\Form;
use brand\tools\Functions;

class Tabs extends Component
{
    public function _init()
    {
        $this->component = 'tabs';
    }

    public function Form()
    {

        $children = [];
        $options = $this->issetFormValue('tabs_options');
        $tabs_values = $this->issetFormValue('tabs_values') ?: [];
        if (!$options) return null;

        foreach ($options as $index => $option) {

            if (!$option || !is_array($option)) continue;
            $sub_columns = [];
            foreach ($option as $key => $item) {

                if (is_object($item)) {
                    $sub_columns[] = $item;
                    continue;
                }

                $sub_method = $item['form']['method'] ?? "";
                if (!$sub_method) continue;

                $ClassName = Config::BRAND_NAMESPACE . Functions::toCamelCase($sub_method);
                if (class_exists($ClassName)){
                    $ClassObject = new $ClassName($item,$tabs_values);
                    $ClassObject->field = $key;
                    $ClassObject->buildColumns();
                    $sub_columns[] = $ClassObject->Form();
                }

            }
            $children[] = Form::$SurfaceForm->component()->el('el-tab-pane')->props('label', $index)->item(false)->children($sub_columns);

        }

        
        $column = Form::$SurfaceForm->column('', $this->label,'')->el('el-tabs')->children($children);


        return $column;
    }
}